<template>
  <div>
    <!--面包屑导航区域-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片视图区域-->
    <el-card class="box-card">
      <!--搜索与添加区域-->
      <el-row :gutter="15">
        <el-col :span="8">
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary">添加用户</el-button>
        </el-col>
      </el-row>
      <!--用户列表区域-->
      <el-table :data="userlist" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="username" label="姓名"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column prop="mobile" label="电话"></el-table-column>
        <el-table-column prop="role_name" label="角色"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.state"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!--修改按钮-->
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <!--删除按钮-->
            <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
            <!--分配角色按钮-->
            <el-tooltip effect="dark" content="分配角色" placement="top" :enterable="false">
              <el-button type="warning" icon="el-icon-setting" size="mini"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!--分页区-->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[1, 2, 5, 10]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //获取用户列表的ajax参数
      queryInfo: {
        query: '',
        // 当前页数
        pagenum: 1,
        // 当前页面数据量
        pagesize: 2
      },
      userlist: [],
      total: 0
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    async getUserList() {
      // 没有后台接口 下模拟测试
      // const { data: res } = await this.$http.getUserList('users', {
      //   params: this.queryInfo
      // })
      const res = {
        data: {
          pagenum: 1,
          total: 4,
          users: [
            {
              id: 500,
              role_name: '超级管理员',
              username: 'admin',
              create_time: 1486720211,
              mobile: 13633822725,
              email: '1813045878@qq.com',
              state: true
            },
            {
              id: 502,
              role_name: '测试角色',
              username: 'test',
              create_time: 1486720211,
              mobile: 13633822725,
              email: '1813045878@qq.com',
              state: false
            }
          ]
        },
        msg: '获取用户列表成功！',
        status: 200
      }
      if (res.status !== 200){
        return this.$message.error('用户列表获取失败！')
      }
      this.userlist = res.data.users
      this.total = res.data.total
      console.log(res)
    },
    // 监听pagesize改变事件  选择页显示 数据量
    handleSizeChange(newSize){
      this.queryInfo.pagesize = newSize
      // 暂时没有接口，不能请求。可以主动请求接口 获取指定数量的数据
      this.getUserList()
      console.log(newSize)
    },
    // 监听 页码值 改变的事件。 点击页码功能
    handleCurrentChange(newPage){
      this.queryInfo.pagenum = newPage
      this.getUserList()
      console.log(newPage)
    }
  }
}
</script>

<style lang="less" scoped>
</style>
